﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vngoodlife.Singleton;

namespace vngoodlife.Models
{
    public class TypesOfArticleModels
    {
        /// <summary>
        /// insert typeID to Table TypesOfArticle
        /// </summary>
        /// <param name="ty"></param>
        internal void InsertType(TypesOfArticle ty)
        {
            try
            {
                if (!DataSingleton.Instance.GetLinQContext().TypesOfArticles.Any(u => u == ty))
                {
                    DataSingleton.Instance.GetLinQContext().TypesOfArticles.InsertOnSubmit(ty);
                    DataSingleton.Instance.GetLinQContext().TypesOfArticles.Context.SubmitChanges();
                }
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }

        /// <summary>
        /// delete a record in table TypesOfArticle
        /// </summary>
        /// <param name="articleTypeID"></param>
        /// <param name="articleID"></param>
        internal void DeleteTypesOfArticle(int articleTypeID, int articleID)
        {
            try
            {
                var ty = DataSingleton.Instance.GetLinQContext().TypesOfArticles
                        .Single(u => u.ArticleID == articleID && u.ArticleTypeID == articleTypeID);
                DataSingleton.Instance.GetLinQContext().TypesOfArticles.DeleteOnSubmit(ty);
                DataSingleton.Instance.GetLinQContext().TypesOfArticles.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }

        internal bool InsertManyType(int id, string articleTypeIDs)
        {
            try
            {
                DataSingleton.Instance.GetLinQContext().sp_TypesOfArticleInsert(id, articleTypeIDs);
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
                return false;
            }
            return true;
        }
    }
}